package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.DataSet;
import com.yeshine.opendata.entity.DataTheme;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.service.IDataSetService;
import com.yeshine.opendata.service.IDataThemeService;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * Created by taohj on 2014/7/10.
 */
@Controller
@RequestMapping("/admin/dataset")
public class AdminDataSetController extends AbstractFrontControllerBase {


    @Autowired
    private IDataSetService service;

    @Autowired
    private IOrgService orgService;




    @Autowired
    private IDataThemeService dataThemeService;


    @RequestMapping("createV")
    public String createV(){
        List<DataTheme> themes=dataThemeService.getList(null).getList();
        DataTheme dataTheme=new DataTheme(null);
        dataTheme.setName("请选择");
        themes.add(0,dataTheme);
        setRequestAttr("themes", themes);


        List<Org> orgs=orgService.getList(null).getList();
        Org org=new Org(null);
        org.setName("请选择");
        orgs.add(0,org);
        setRequestAttr("orgs",orgs);
        return "admin/dataset/create";
    }


    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam("name") String name,
            @RequestParam("description") String description,
            @RequestParam("theme") Integer theme,
            @RequestParam(value = "org",required = false) Integer org
    ) {
        DataSet dataSet = new DataSet();
        dataSet.setName(name);
        dataSet.setDescription(description);
        dataSet.setCreater(getCurrentUser());
        dataSet.setTheme(new DataTheme(theme));
        if (org != null) {
            dataSet.setOrg(new Org(org));
        }
        Result<DataSet> result = service.create(dataSet);

        return result.toString();
    }



    @ResponseBody
    @RequestMapping("remove")
    public  String remove(
            @RequestParam(value = "id",required = true) Integer id
    ){
        Result<DataSet> result=service.remove(new DataSet(id));
           return  result.toString();

    }



    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam(value = "id", required = true) Integer id
    ) {
        DataSet dataSet=service.getById(id).getTarget();
        setRequestAttr("dataSet", dataSet);


        List<DataTheme> themes=dataThemeService.getList(null).getList();
        DataTheme dataTheme=new DataTheme(null);
        dataTheme.setName("请选择");
        themes.add(0,dataTheme);
        setRequestAttr("themes", themes);


        List<Org> orgs=orgService.getList(null).getList();
        Org org=new Org(null);
        org.setName("请选择");
        orgs.add(0,org);
        setRequestAttr("orgs",orgs);
        return "admin/dataset/modify";
    }


    @ResponseBody
    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("description") String description,
            @RequestParam("theme") Integer theme,
            @RequestParam(value = "org",required = false) Integer org
    ){
        DataSet dataSet=service.getById(id).getTarget();
        dataSet.setId(id);
        dataSet.setName(name);
        dataSet.setTheme(new DataTheme(theme));
        dataSet.setDescription(description);
        dataSet.setOrg(new Org(org));
        Result<DataSet>  result=service.modify(dataSet);
        return result.toString();
    }






    @RequestMapping("listV")
    public  String listV(){
        return "admin/dataset/list";
    }


    @ResponseBody
    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "order",required = false) String order,
            @RequestParam(value = "id",required = false) Integer id,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "org",required = false) Integer org,
            @RequestParam(value = "theme",required = false) Integer theme
    ) {
        PageResult<DataSet> pageResult = new PageResult<DataSet>();
        pageResult.setSize(size);
        pageResult.setPage(page);
        DataSet dataSet = new DataSet();

        if (id!=null){
            dataSet.setId(id);
        }

        if(org!=null){
            dataSet.setOrg(new Org(org));
        }

        if (theme!=null){
            dataSet.setTheme(new DataTheme(theme));
        }

        if (StringUtils.hasLength(name)){
            dataSet.setName(name);
        }
        if (StringUtils.hasLength(order)){
            pageResult.setSortBy(order);
            pageResult.setSortColumn("ds.name");
        }

        pageResult.setT(dataSet);
        Result<DataSet> result = service.getByPage(pageResult);
        return result.getPageResult().toString();
    }


}
